<?php

/*
    Auditorium controller is used to view, add and edit cinema auditoriums.
    Functionallity can be accessed only by admins.
 */

class Controller_Auditorium extends Controller_Template {
    private $user_data;
    private $user_id;
    private $language;
    
    public function before() {
        if(!Auth::member(100)) {
            Response::redirect('/');
        }
        
        $this->language = Cookie::get((Auth::check() ? 'user' : 'guest') . '_language', 'lv');
        Config::set("language", $this->language);
        Lang::load('main', null, $this->language, true);
        
        parent::before();
        $this->user_data = Auth::instance();
        $this->user_id = $this->user_data->get_user_id();
        
        $this->template->language = $this->language;
        $this->template->genres = Model_Orm_Genre::find('all');
        $this->template->cinema_info = Model_Orm_Cinema::find('first');
    }
    
    public function action_add() {      
        if(Input::method() === 'POST') {   
            $auditorium_name = Input::post('Auditorium_Name');
            
            //Looking for auditoriums with the same name.
            $auditorium_name_exists = DB::select()->from('auditoriums')
                ->where('auditorium_name', 'LIKE', $auditorium_name)
                ->execute()->as_array();
            
            $validation = Model_Orm_Auditorium::validate('add');
            
            if($validation->run() && count($auditorium_name_exists) == 0) {
                $new_auditorium = new Model_Orm_Auditorium();
                $new_auditorium->auditorium_name = $validation->validated('Auditorium_Name');
                $new_auditorium->rows = $validation->validated('Rows');
                $new_auditorium->seats_per_row = $validation->validated('Seats');
                $new_auditorium->support_3d = Input::post('Support_3D') ? 1 : 0;
                
                $new_auditorium->save();
                Session::set_flash('success', 'Skatītāju zāle veiksmīgi pievienota !');
            } else {
                Session::set_flash('error', 'Ievaddati nepareizi vai skatītāju zāle eksistē ar norādīto nosaukumu !');
                Response::redirect('auditorium/add');
            }
        }
        
        $view_auditorium_add = View::forge('auditorium/add');
        $this->template->page_content = $view_auditorium_add;
    }
    
    public function action_list() {
        $auditoriums = Model_Orm_Auditorium::find('all');
        
        $view_auditorium_list = View::forge('auditorium/list');
        $view_auditorium_list->set('auditorium_data', $auditoriums);
        $this->template->page_content = $view_auditorium_list;
    }
    
    public function action_edit($auditorium_id = null) {
        is_null($auditorium_id) and Response::redirect('/');
        
        $auditorium_data = Model_Orm_Auditorium::find($auditorium_id);
        
        //If auditorium with the specified ID doesn't exist, redirect to main page
        is_null($auditorium_data) and Response::redirect('/');
        
        if(Input::method() == 'POST') {
            $auditorium_name = Input::post('Auditorium_Name');
            
            $auditorium_name_exists = DB::select()->from('auditoriums')
                ->where('auditorium_name', 'LIKE', $auditorium_name)
                ->execute();
            $validation = Model_Orm_Auditorium::validate('edit');
            
            if($validation->run() && empty($auditorium_name_exists)) {
                $auditorium_data->auditorium_name = $validation->validated('Auditorium_Name');
                $auditorium_data->rows = $validation->validated('Rows');
                $auditorium_data->seats_per_row = $validation->validated('Seats');
                $auditorium_data->support_3d = Input::post('Support_3D') ? 1 : 0;
                $auditorium_data->save();
                Session::set_flash('success', 'Skatītāju zāles informācija saglabāta !');
            } else {
                Session::set_flash('error', 'Ievaddati nepareizi vai skatītāju zāle eksistē ar norādīto nosaukumu !');
                Response::redirect('auditorium/edit/'.$auditorium_id);
            }
        }
        
        $view_auditorium_edit = View::forge('auditorium/edit');
        $view_auditorium_edit->set('auditorium_data', $auditorium_data);
        $this->template->page_content = $view_auditorium_edit;
    }
}
